package liang.jiaxin.com.exception;


import liang.jiaxin.com.dto.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 */
@ControllerAdvice
public class MyExceptionHandler {

    public static final String ERROR_VIEW = "404";

    @ExceptionHandler(value = BusinessException.class)//指定拦截的异常
    @ResponseBody
    public ResponseEntity errorHandler(HttpServletRequest request, HttpServletResponse response, Exception e){
        e.printStackTrace();//打印异常信息

        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.setStatus(500);
        responseEntity.setOk(false);
        responseEntity.setMessage(e.getMessage());
        responseEntity.setData(null);

//        ModelAndView mv = new ModelAndView();
//        mv.addObject("exception",e);
//        mv.addObject("url",request.getRequestURL());//发生异常的路径
//        mv.setViewName(ERROR_VIEW);//指定发生异常之后跳转页面
        return responseEntity;
    }
}